package com.neuedu.shixun.hospital2.service.Impl;
import com.neuedu.shixun.hospital2.entity.*;
import com.neuedu.shixun.hospital2.repository.BingRepository;
import com.neuedu.shixun.hospital2.repository.DoctorRepository;
import com.neuedu.shixun.hospital2.repository.KsRepository;
import com.neuedu.shixun.hospital2.repository.OrderRepository;
import com.neuedu.shixun.hospital2.service.DoctorService;
import com.neuedu.shixun.hospital2.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class DoctorServiceImpl implements DoctorService{
    @Autowired
    DoctorRepository doctorRepository;
    @Autowired
    KsRepository ksRepository;
    @Autowired
    BingRepository bingRepository;
    @Autowired
    OrderRepository orderRepository;
    @Override
    public String checkOne(String idNum,String aPassword,String name) {
        log.info("参数："+idNum+"  "+aPassword+"  "+name);
        Doctor doctor=doctorRepository.findByDJobnumAndDPasswordAndDDname(idNum, aPassword,name);
        log.info("doctor:"+doctor);
        if (doctor!=null){
           if (doctor.getDStatus())
            return "ok";
           else
               return "no";
        }
        else
            return "false";
    }
    @Override
    public String save(RuleForm2 ruleForm2) {
        Doctor doctor=new Doctor();
        doctor.setDDname(ruleForm2.getName());
        doctor.setDJobnum(ruleForm2.getIdNum());
        try {
            doctor.setDPassword(MD5Util.EncoderByMd5(ruleForm2.getPass()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        doctor.setDPassword(ruleForm2.getPass());
        doctor.setDPhonenum(ruleForm2.getTel());
        doctor.setDStatus(ruleForm2.getAstatus());
        doctor.setZKid(3);
        if(doctorRepository.save(doctor)!=null){
            log.info("注册的doctor"+doctorRepository.save(doctor));
           return "ok";
        }
        else
            return "false";
    }
    @Override
    public List<Doctor> getDoctor() {
       List<Doctor> list=doctorRepository.findAll();
       return list;
    }
    @Override
    public List<Doctor2> getDoctor2(List<Doctor> list) {
        List<Doctor2> list1=new ArrayList<>();
        for (Doctor doctor:list){
            Doctor2  doctor2=new Doctor2();
            doctor2.setDDid(doctor.getDDid());
            doctor2.setDDName(doctor.getDDname());
            doctor2.setDAge(doctor.getDAge());
            doctor2.setDSex(doctor.getDSex());
            doctor2.setDAddress(doctor.getDAddress());
            doctor2.setDJobnum(doctor.getDJobnum());
            doctor2.setDpassword(doctor.getDPassword());
            log.info("科室id："+doctor.getKKid());
            try{
            Optional<Ks> optionalKs= ksRepository.findById(doctor.getKKid());
            Ks ks=new Ks();
            if (optionalKs.isPresent()){
             ks=optionalKs.get();
          }
                doctor2.setDKname(ks.getKKname());
                JiBing jiBing=new JiBing();
                Optional<JiBing> optional=bingRepository.findById(doctor.getBBid());
                if (optional.isPresent()){
                    jiBing=optional.get();
                }
                doctor2.setBBname(jiBing.getJbname());
            }
            catch (Exception e){
            }
            doctor2.setDHead(doctor.getDHead());
            doctor2.setDPhonenum(doctor.getDPhonenum());
            doctor2.setDTitle(doctor.getDTitle());
            doctor2.setDStatus(doctor.getDStatus());
            list1.add(doctor2);
        }
        return list1;
    }

    @Override
    public Doctor getOne(String jobNum) {
        return doctorRepository.findByDJobnum(jobNum);
    }

    @Override
    public Doctor update(Doctor doctor) {
        doctor.setZKid(3);
        return doctorRepository.save(doctor);
    }

    @Override
    public Doctor2 getOne2(Doctor doctor) {
        Doctor2  doctor2=new Doctor2();
        doctor2.setDDid(doctor.getDDid());
        doctor2.setDDName(doctor.getDDname());
        doctor2.setDAge(doctor.getDAge());
        doctor2.setDSex(doctor.getDSex());
        doctor2.setDAddress(doctor.getDAddress());
        doctor2.setDJobnum(doctor.getDJobnum());
        doctor2.setDpassword(doctor.getDPassword());
        doctor2.setIdcard(doctor.getDIdcard());
        doctor2.setDPhonenum(doctor.getDPhonenum());
        doctor2.setIdcard(doctor.getDIdcard());
        log.info("科室id："+doctor.getKKid());
        try {
            Optional<Ks> optionalKs= ksRepository.findById(doctor.getKKid());
            Ks ks=new Ks();
            if (optionalKs.isPresent()){
                ks=optionalKs.get();
            }
            doctor2.setDKname(ks.getKKname());
            JiBing jiBing=new JiBing();
            Optional<JiBing> optional=bingRepository.findById(doctor.getBBid());
            if (optional.isPresent()){
                jiBing=optional.get();
            }
            doctor2.setBBname(jiBing.getJbname());
        }catch (Exception e){

        }

        doctor2.setDHead(doctor.getDHead());
//        doctor2.setDPhonenum(doctor.getDPhonenum());
        doctor2.setDTitle(doctor.getDTitle());
        doctor2.setDStatus(doctor.getDStatus());
        return doctor2;
    }

    @Override
    public String Jiami(String pass) {
        String pass1= null;
        try {
            pass1 = MD5Util.EncoderByMd5(pass);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pass1;
    }


}
